package com.RequestGongWuShu;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import com.yishun.springboot.pojo.ElectricityMonitoringGetdata;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ElectricityMonitoring {

	private static String baseUlr = "http://szwb.sz.gov.cn:2018/openapi";// 请求地址

	/**
	 * 上传配电箱基础数据到工务署
	 * 
	 * @param param
	 * @param api_key
	 * @param engCode
	 * @param projectCode
	 * @return
	 */
	public static String uploadDetailed(JSONObject body, String api_key, String engCode, String projectCode) {
		JSONObject param = new JSONObject();

		String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

		String url = baseUlr + "/powerBox/addParams";

		String bodyStr = JSONObject.toJSONString(body);

		String signature = getSignature(api_key, "1.0", engCode, projectCode, timestamp, bodyStr);

		param.put("api_key", api_key);
		param.put("eng_code", engCode);
		param.put("project_code", projectCode);
		param.put("timestamp", timestamp);
		param.put("api_version", "1.0");
		param.put("body", body);
		param.put("signature", signature);

		log.info("最终发送参数" + param.toString());

		String resInfo = HttpUtil.post(url, param.toJSONString());

		return resInfo;
	}

	/**
	 * 上传配电箱基础数据到工务署
	 * 
	 * @param param
	 * @param api_key
	 * @param engCode
	 * @param projectCode
	 * @return
	 */
	public static String uploadData(JSONObject body, String api_key, String engCode, String projectCode) {
		JSONObject param = new JSONObject();

		String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

		String url = baseUlr + "/powerBox/addRecord";
		List<JSONObject> bodyList = new ArrayList<JSONObject>();
		bodyList.add(body);

		String bodyStr = JSONObject.toJSONString(bodyList);

		String signature = getSignature(api_key, "1.0", engCode, projectCode, timestamp, bodyStr);

		param.put("api_key", api_key);
		param.put("eng_code", engCode);
		param.put("project_code", projectCode);
		param.put("timestamp", timestamp);
		param.put("api_version", "1.0");
		param.put("body", bodyList);
		param.put("signature", signature);

		log.info("最终发送参数" + param.toString());

		String resInfo = HttpUtil.post(url, param.toJSONString());

		return resInfo;
	}

	public static void main(String[] args) {
//		JSONObject body = new JSONObject();
//		body.put("device_no", UUID.randomUUID().toString().replaceAll("-", ""));//5e06e91f398f4a5388975ef1a1f596fb
//		body.put("type", 1);
//		body.put("name", "1#配电箱");
//		body.put("install_company", "一顺科技有限公司");
//		body.put("installadd_type", 1);
//
//		body.put("temp_limit", 80.0);
//		body.put("elec_limit", 3000);
//		body.put("around_temp", 80.0);

		// log.info(uploadDetailed(body, "a23dad6803a6449389990a042af99789",
		// "HG001267", "XM20170385"));

		String operate_time = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

		JSONObject body = new JSONObject();
		body.put("device_no", "5e06e91f398f4a5388975ef1a1f596fb");// 5e06e91f398f4a5388975ef1a1f596fb
		body.put("operate_time", operate_time);
		body.put("status", 1);
		body.put("warn_type", 0);

		body.put("cable_a_temp", 28.5);
		body.put("cable_b_temp", 28.2);
		body.put("cable_c_temp", 28.3);

		body.put("elec_limit", 101);
		body.put("env_temp", 28.5);

		// log.info(uploadData(body, "a23dad6803a6449389990a042af99789",
		// "HG001267", "XM20170385"));
	}

	/**
	 * 获取签名
	 *
	 * @param: [apiKey, apiVersion, engCode, projectCode, timestamp, body]
	 * @return: java.lang.String
	 */
	public static String getSignature(String apiKey, String apiVersion, String engCode, String projectCode,
			String timestamp, String body) {
		String str4MD5 = String.format("api_key%sapi_version%sbody%seng_code=%sproject_code=%stimestamp%s", apiKey,
				apiVersion, body.isEmpty() ? "{}" : body, engCode, projectCode, timestamp);
		return getMD5Code(str4MD5);
	}

	public static String getMD5Code(String strObj) {
		String resultString = null;
		try {
			resultString = new String(strObj);
			MessageDigest md = MessageDigest.getInstance("MD5");
			Charset charset = Charset.forName("UTF-8");
			resultString = byteToString(md.digest(strObj.getBytes(charset)));
		} catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace();
		}
		return resultString;
	}

	// 转换字节数组为16进制字串
	private static String byteToString(byte[] bByte) {
		StringBuffer sBuffer = new StringBuffer();
		for (int i = 0; i < bByte.length; i++) {
			sBuffer.append(byteToArrayString(bByte[i]));
		}
		return sBuffer.toString();
	}

	private static String byteToArrayString(byte bByte) {
		int iRet = bByte;
		if (iRet < 0) {
			iRet += 256;
		}
		int iD1 = iRet / 16;
		int iD2 = iRet % 16;
		return strDigits[iD1] + strDigits[iD2];
	}

	// 全局数组
	private final static String[] strDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D",
			"E", "F" };

}
